package com.hzlemone.form.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzlemone.form.model.request.FormInfoRequestDTO;
import com.hzlemone.form.service.IFormInfoService;
import com.hzlemone.mapper.model.FormInfoMapper;
import com.hzlemone.mapper.model.db.FormInfoDO;
import com.hzlemone.utils.CollectionMapperDecorator;

/**
 * @ClassName FormInfoServiceImpl.java
 * @Description 表单信息服务
 * @author Francis.LW
 * @date 2017年11月13日
 **/

@Service
@Transactional
public class FormInfoServiceImpl implements IFormInfoService {

	@Autowired
	CollectionMapperDecorator collectionMapperDecorator;

	@Autowired
	FormInfoMapper formInfoMapper;

	@Override
	public List<FormInfoDO> listFormInfo(FormInfoDO formInfoDO) {
		return formInfoMapper.listFormInfo(formInfoDO);
	}

	@Override
	public PageInfo<FormInfoDO> listFormInfoByPage(FormInfoDO param) {
		if (param.getPage() != null && param.getRows() != null) {
			PageHelper.startPage(param.getPage(), param.getRows(),
					param.getSort());
		}
		List<FormInfoDO> formInfoDOs = formInfoMapper.listFormInfo(param);
		PageInfo<FormInfoDO> p = new PageInfo<FormInfoDO>(formInfoDOs);
		return p;
	}

	@Override
	public FormInfoDO getFormInfoById(String id) {
		return formInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveFormInfo(FormInfoRequestDTO formInfoRequestDTO) {

		FormInfoDO formInfoDO = collectionMapperDecorator.map(
				formInfoRequestDTO, FormInfoDO.class);
		formInfoDO.setCreatedTime(new Date());

		formInfoMapper.insert(formInfoDO);
	}

	@Override
	public void updateFormInfo(String id, FormInfoRequestDTO formInfoRequestDTO) {
		FormInfoDO formInfoDO = collectionMapperDecorator.map(
				formInfoRequestDTO, FormInfoDO.class);

		formInfoDO.setId(id);
		formInfoMapper.updateByPrimaryKeySelective(formInfoDO);
	}

	@Override
	public void removeFormInfo(String[] ids) {
		for (String id : ids) {
			formInfoMapper.deleteByPrimaryKey(id);
		}
	}

}
